package com.commpany.servie.basic.tag.service.read;

import com.commpany.servie.basic.tag.api.read.ProductTagMapReadService;
import com.commpany.servie.basic.tag.model.dto.producttagmap.ProductTagMapDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.ArrayList;
import com.commpany.servie.basic.tag.api.read.ProductTagReadService;
import com.commpany.jdq.core.common.PageResult;
import com.commpany.jdq.core.orm.Pagination;
import com.commpany.servie.basic.tag.business.read.productTag.ProductTagReadManage;
import com.commpany.servie.basic.tag.model.dto.producttag.ProductTagDTO;
import com.commpany.servie.basic.tag.converter.ProductTagConverter;
import com.commpany.servie.basic.tag.po.ProductTagPO;
import com.commpany.servie.basic.tag.model.dto.PageResultDTO;
@Service("productTagReadService")
public class ProductTagReadServiceImpl  implements ProductTagReadService {
	@Autowired
	private ProductTagReadManage productTagReadManage;

	private ProductTagMapReadService productTagMapReadService;
	public ProductTagDTO getProductTagService(ProductTagDTO inputDTO){
	
	   return ProductTagConverter.toDTO(productTagReadManage.getProductTagService(ProductTagConverter.toPO(inputDTO)));
	}
   @Override
	public PageResultDTO<ProductTagDTO> selectProductTagPageService(ProductTagDTO inputDTO){

		ProductTagPO po = ProductTagConverter.toPO(inputDTO);
		Pagination page = new Pagination();
	    page.setLimitStart(inputDTO.getLimitStart());
		page.setPageNo(inputDTO.getPageNo());
		page.setPageSize(inputDTO.getPageSize());
		Optional.ofNullable(inputDTO.getOrderBy()).ifPresent(page::setOrderBy);
		PageResult<ProductTagPO> poPage = productTagReadManage.selectProductTagPageService( po,page);
		List<ProductTagPO> poList = poPage.getList();
		if(Objects.isNull(poList)) {
	    	 return PageResultDTO.<ProductTagDTO>builder().build();
	     }		
		 List<ProductTagDTO> dtoList = poList.stream().map(src-> ProductTagConverter.toDTO(src)).collect(Collectors.toList());
		return 	PageResultDTO.<ProductTagDTO>builder().pageNo(poPage.getPageNo()).pageSize(poPage.getPageSize()).list(dtoList).build();
	}
   @Override
    public int countProductTagService( ProductTagDTO inputDTO ){
         ProductTagPO po = ProductTagConverter.toPO(inputDTO);
         return productTagReadManage.countProductTagService(po);
    }
     @Override
	public List<ProductTagDTO> queryProductTagService (ProductTagDTO inputDTO){
	    ProductTagPO po = ProductTagConverter.toPO(inputDTO);
	     List<ProductTagPO> list = productTagReadManage.queryProductTagService(po);
		   if(Objects.isNull(list)) {
	    	 return new ArrayList<ProductTagDTO>();
	     }
		return list.stream().map(src-> ProductTagConverter.toDTO(src)).collect(Collectors.toList());
	}

	@Override
	public List<String> queryProductTagsByProductID(Integer productId) {
		//根据产品ID查询该产品对应到产品tag列表
		List<ProductTagMapDTO> tagsList = productTagMapReadService.queryProductTagMapService(ProductTagMapDTO.builder().productId(productId).build());
		//查询tagId对应到tag名称
		List<String> tagsNameList = tagsList.stream().map(tag ->
				productTagReadManage.getProductTagService(ProductTagPO.builder().id(tag.getProductTagId()).build()).getTagName()
		).collect(Collectors.toList());
		return tagsNameList;
	}
}
	